//
//  NSModelHotView.m
//  Meboo
//
//  Created by kyo on 2024/5/15.
//

#import "NSModelHotView.h"
#import "MSWeakTimer.h"
@interface NSModelHotView ()
@property (nonatomic, strong) UIImageView *backImageView;
@property (nonatomic, strong) UIButton *timeBtn;
//@property (nonatomic, strong) dispatch_source_t timer;

@property (nonatomic, strong) MSWeakTimer *countDownTimer;
@property (nonatomic, strong) UILabel *videoLengthLab;
@end

@implementation NSModelHotView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)updateMessageNum {
    if (self.second > 0) {
        self.second = self.second -1;
        [self setTimeStr];
    }
}

//定时器注销
- (void)stopTimer {
    [self.countDownTimer invalidate];
    self.countDownTimer = nil;
}

#pragma mark - Public Metohd
- (void)setSecond:(NSInteger)second {
    _second = second;
    self.backImageView.hidden = NO;
    self.timeBtn.hidden = NO;
//    [MBNotificationCenter addObserver:self selector:@selector(updateMessageNum) name:CHAT_REFRESH_TIMER object:nil];
    if (second > 0) {
        [self setTimeStr];
        [self.countDownTimer invalidate];
        self.countDownTimer = nil;
        self.countDownTimer = [MSWeakTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChanged:) userInfo:nil repeats:YES dispatchQueue:dispatch_get_main_queue()];
//        self.countDownTimer = [MSWeakTimer scheduledTimerWithTimeInterval:1
//                                         target:self
//                                       selector:@selector(timeChanged:)
//                                       userInfo:nil
//                                        repeats:YES];
    }

//    [[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];
    //    if (self.timer) {
    //        dispatch_source_cancel(self.timer);
    //        self.timer = nil;
    //    }
    
    //    __weak typeof(self) weakSelf = self;
    //    self.timer = [GCDTimer scheduledTimerWithTimeInterval:1000000000 queue:dispatch_get_main_queue() action:^{
    //        if (weakSelf.second<=0) {
    //            dispatch_source_cancel(weakSelf.timer);
    //            weakSelf.timer = nil;
    //            return;
    //        }
    //            if (self.second < 60) {
    //                NSString *sec = [NSString stringWithFormat:@"%ld",self.second];
    //                if (sec.length == 1) {
    //                    sec = [NSString stringWithFormat:@"0%@",sec];
    //                }
    //                [self.timeBtn setTitle:[NSString stringWithFormat:@"00:00:%@",sec] forState:0];
    //            } else if (self.second < 3600) {
    //                NSString *sec = [NSString stringWithFormat:@"%ld",self.second%60];
    //                if (sec.length == 1) {
    //                    sec = [NSString stringWithFormat:@"0%@",sec];
    //                }
    //                NSString *min = [NSString stringWithFormat:@"%ld",self.second/60];
    //                if (min.length == 1) {
    //                    min = [NSString stringWithFormat:@"0%@",min];
    //                }
    //                [self.timeBtn setTitle:[NSString stringWithFormat:@"00:%@:%@",min,sec] forState:0];
    //            }
    //        weakSelf.second -= 1;
    //        NSLog(@"******%ld",weakSelf.second);
    //    }];
    
}

- (void)setVideoLength:(NSInteger)videoLength {
    _videoLength = videoLength;
    self.videoLengthLab.hidden = NO;
    self.playImageView.hidden = NO;
    [self videoLengthTime];
}

#pragma mark - Action
- (void)timeChanged:(NSTimer *)timer {
    
    if (self.second <= 0) {
        if (self.timeEndBlock) {
            self.timeEndBlock();
        }
        [timer invalidate];
        timer = nil;
        return;
    } else if (self.second < 60) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.second];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        [self.timeBtn setTitle:[NSString stringWithFormat:@"00:00:%@",sec] forState:0];
    } else if (self.second < 3600) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.second%60];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        NSString *min = [NSString stringWithFormat:@"%ld",self.second/60];
        if (min.length == 1) {
            min = [NSString stringWithFormat:@"0%@",min];
        }
        [self.timeBtn setTitle:[NSString stringWithFormat:@"00:%@:%@",min,sec] forState:0];
    }
    if (timer) {
        NSLog(@"******%ld",self.second);
    }
    
    self.second--;
    if (self.second <= 0) {
        [timer invalidate];
    }
}
- (void)setTimeStr{
    if (self.second < 60) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.second];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        [self.timeBtn setTitle:[NSString stringWithFormat:@"00:00:%@",sec] forState:0];
    } else if (self.second < 3600) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.second%60];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        NSString *min = [NSString stringWithFormat:@"%ld",self.second/60];
        if (min.length == 1) {
            min = [NSString stringWithFormat:@"0%@",min];
        }
        [self.timeBtn setTitle:[NSString stringWithFormat:@"00:%@:%@",min,sec] forState:0];
    }
}

- (void)videoLengthTime {
    if (self.videoLength < 60) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.videoLength];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        self.videoLengthLab.text = [NSString stringWithFormat:@"00:%@",sec];
    } else if (self.videoLength < 3600) {
        NSString *sec = [NSString stringWithFormat:@"%ld",self.videoLength%60];
        if (sec.length == 1) {
            sec = [NSString stringWithFormat:@"0%@",sec];
        }
        NSString *min = [NSString stringWithFormat:@"%ld",self.videoLength/60];
        if (min.length == 1) {
            min = [NSString stringWithFormat:@"0%@",min];
        }
        self.videoLengthLab.text = [NSString stringWithFormat:@"%@:%@",min,sec];
    }
}

//- (void)actionUpdateCountDownNumber {
//    if (self.second > 0) {
//        if (self.second < 60) {
//            NSString *sec = [NSString stringWithFormat:@"%ld",self.second];
//            if (sec.length == 1) {
//                sec = [NSString stringWithFormat:@"0%@",sec];
//            }
//            [self.timeBtn setTitle:[NSString stringWithFormat:@"00:00:%@",sec] forState:0];
//        } else if (self.second < 3600) {
//            NSString *sec = [NSString stringWithFormat:@"%ld",self.second%60];
//            if (sec.length == 1) {
//                sec = [NSString stringWithFormat:@"0%@",sec];
//            }
//            NSString *min = [NSString stringWithFormat:@"%ld",self.second/60];
//            if (min.length == 1) {
//                min = [NSString stringWithFormat:@"0%@",min];
//            }
//            [self.timeBtn setTitle:[NSString stringWithFormat:@"00:%@:%@",min,sec] forState:0];
//        }
//    } else {
//        [self.countDownTimer invalidate];
//    }
//    self.second -= 1;
//}

#pragma mark - UI
- (void)setupUI {
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(MBHeightScale(29));
    }];
    
    [self addSubview:self.timeBtn];
    [self.timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.backImageView);
    }];
    
    [self addSubview:self.playImageView];
    [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY);
        make.centerX.equalTo(self);
    }];
    
    [self addSubview:self.videoLengthLab];
    [self.videoLengthLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_centerY);
        make.centerX.equalTo(self);
    }];
}

#pragma mark - Setter && Getter
- (UIButton *)timeBtn {
    if (!_timeBtn) {
        _timeBtn = [UIButton leftimageAndRighttitlePatternWithImage:@"chat_private_hot" AndMargin:2 AndTitle:@"00:59:59" AndTitleFont:MBHelFontSizeScale(14) AndTitleColor:UIColor.whiteColor];
        _timeBtn.hidden = YES;
    }
    return _timeBtn;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView new];
        _backImageView.image = ImageWithName(@"chat_private_back");
        _backImageView.hidden = YES;
    }
    return _backImageView;
}

- (UILabel *)videoLengthLab {
    if (!_videoLengthLab) {
        _videoLengthLab = [UILabel new];
        _videoLengthLab.textColor = UIColor.whiteColor;
        _videoLengthLab.font = MBHelFontSizeScale(14);
        _videoLengthLab.hidden = YES;
    }
    return _videoLengthLab;
}

- (UIImageView *)playImageView {
    if (!_playImageView) {
        _playImageView = [UIImageView new];
        _playImageView.image = ImageWithName(@"chat_sight_message_icon");
        _playImageView.hidden = YES;
    }
    return _playImageView;
}

- (void)dealloc {
//    NSLog(@"%@---%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
//    NSLog(@"%s",__func__);
//    NSLog(@"%s---%@",object_getClassName(self),NSStringFromSelector(_cmd));
//    if (self.timer) {
//        dispatch_source_cancel(self.timer);
//        self.timer = nil;
//    }
    
    [self.countDownTimer invalidate];
}
@end
